package software.hust.com.hust_last.util.oss;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import java.io.File;
import java.lang.ref.WeakReference;

import software.hust.com.hust_last.R;

public class OssImageCallback extends OssCallback {
    private WeakReference<ImageView> mImageViewRef;

    public OssImageCallback(ImageView imageView) {
        mImageViewRef = new WeakReference<>(imageView);
    }

    @Override
    public void onSuccess(String url, File file) {
        ImageView imageView = mImageViewRef.get();
        if (imageView != null && url.equals(imageView.getTag())) {
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            OssFileOperator.putToImageCache(url, bitmap);
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onProgress(long currentSize, long totalSize) {
    }

    @Override
    public void onFailure(String errMsg) {
        ImageView imageView = mImageViewRef.get();
        if (imageView != null) {
            imageView.setImageResource(R.drawable.test2);
        }
    }
}
